package com.bk.cyxf.lx;

import com.bk.cyxf.log.LogManager;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.purview.PurviewManager;
import jt.classic.system.role.IRole;
import jt.classic.system.role.RoleManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DataTrimmer;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.DateTrimmer;
import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexItem;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 来信办理
 *
 * @author Mido
 *
 */
public class LxblAction extends SimpleAction {
    /**
     * 来信办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String status = StringTool.checkString(info.get("BLSTATUS"));

                if (status.equals("3")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来信登记已办结,不能重复办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 区信办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editqu(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String status = StringTool.checkString(info.get("BLSTATUS"));

                if (status.equals("3")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来信登记已办结,不能重复办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxblqu");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信审批
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editsp(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String status = StringTool.checkString(info.get("SHSTATUS"));

                if (status.equals("2")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来信登记已办结,不能重复审批");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxsp");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edithdl(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LXID"));

                String status = StringTool.checkString(info.get("LXSTATUS"));

                if (status.equals("1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "来信登记已办结,不能重复办理");
                    forward = mapping.findForward("error");
                } else {
                    String sql = "SELECT * FROM APP_XFHANDLER WHERE XFID = ? AND XF_TYPE = ?";

                    DataTrimmerI trimmer = new DataTrimmerI(conn);

                    DataTrimmerI.DataItemList dil = trimmer.createDIL();
                    dil.set(lxid);
                    dil.set(0);

                    Map hdlMap = trimmer.searchSingleData(sql, dil);

                    String hdluserid = StringTool.checkString(hdlMap.get(
                                "H_USERID"));

                    IUser user = ISystem.getSessionUser(request);

                    if (!hdluserid.equals("") && !hdluserid.equals(user.id())) {
                        request.setAttribute("error", "来信已在办理过程中...");
                        forward = mapping.findForward("error");
                    } else {
                        if (hdluserid.equals("")) {
                            String hdlkey = StringTool.checkString(hdlMap.get(
                                        "HID"));

                            SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
                                    "APP_XFHANDLER");

                            /*设置办理人ID*/
                            sbt1.getResource().put("H_USERID", user.id());

                            /*设置办理人姓名*/
                            sbt1.getResource().put("H_USERNAME", user.iname());

                            sbt1.getResource().put("HID", hdlkey);

                            BaseworkUtil util = new BaseworkUtil();

                            Map m = util.manualUpdate1(sbt1, conn);

                            conn.commit();
                        }

                        request.setAttribute("object", object);
                        request.setAttribute("wobject", object.iobject());
                        forward = mapping.findForward("lxhdl");
                    }
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
    * 信访办处理来信
    *
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward deal(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String status = StringTool.checkString(request.getParameter("BLSTATUS"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    /*原办理状态*/
                    String orgsts = StringTool.checkString(info.get("BLSTATUS"));

                    /*来信类型*/
                    String LXTYPE = StringTool.checkString(info.get("LXTYPE"));

                    /*自处理*/
                    String stsLabel = "自处理";

                    if (status.equals("2")) {
                        /*办理*/
                        stsLabel = "派遣办理";
                    } else if (status.equals("3")) {
                        /*上报*/
                        stsLabel = "上报处理";
                    } else if (status.equals("-1")) {
                        /*不予办理*/
                        stsLabel = "不予受理";
                    } else if (status.equals("-2")) {
                        /*不予受理*/
                        stsLabel = "不予受理";
                    }

                    CommonBean4HSQ cbh = new CommonBean4HSQ(request);

                    SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_LX",
                            cbh);

                    sbt.getResource().put("lxid", lxid);

                    sbt.getResource().put("PQRID", user.id());
                    sbt.getResource().put("PQRNAME", user.iname());
                    sbt.getResource().put("PQRGROUPID", user.igroup().id());
                    sbt.getResource().put("PQRGROUPNAME", user.igroup().iname());

                    /*不受理*/
                    if (status.equals("-1")) {
                        sbt.getResource().put("LXSTATUS", "1");
                        sbt.getResource().put("BLSTATUS", "-1");
                    } else if (status.equals("1")) {
                    	sbt.getResource().put("BLSTATUS", "1");
                    	
                        String jbrgroupid = StringTool.checkString(request.getParameter(
                                    "H_GROUPID"));

                        String jbrgroupname = StringTool.checkString(request.getParameter(
                                    "H_GROUPNAME"));

                        /*派遣科室办理*/
                        SimpleBean4Table sbt1 = new SimpleBean4TableImpl(
                                "APP_XFHANDLER");

                        /*添加处理科室ID*/
                        sbt1.getResource().put("H_GROUPID", jbrgroupid);

                        /*添加处理科室名称*/
                        sbt1.getResource().put("H_GROUPNAME", jbrgroupname);

                        sbt1.getResource().put("XFID", lxid);
                        sbt1.getResource().put("XF_TYPE", "0");
                        sbt1.getResource().put("H_STATUS", "0");

                        BaseworkUtil util = new BaseworkUtil();

                        Map m = util.manualSave1(sbt1, conn);

                        manager.createSystemRelationObject(id, "APP_XFHANDLER",
                            (String) m.get("HID"));

                        PurviewManager pm = new PurviewManager(conn);

                        RoleManager rm = new RoleManager(conn);

                        IRole role = null;

                        /*街乡信访办理负责人*/
                        if (LXTYPE.equals("0")) {
                            role = rm.getRoleByKey("jxpic");
                        }
                        /*区信访办理负责人*/
                        else if (LXTYPE.equals("1")) {
                            role = rm.getRoleByKey("qupic");
                        }

                        /*授权信访办理负责人可对来信进行办理*/
                        pm.insert(object.id(), object.iname(), object.ikey(),
                            "ISYS_OBJECT", 3, role.id(), role.iname(), "role",
                            user, false);
                    }

                    BaseworkUtil util = new BaseworkUtil();

                    util.manualUpdate1(sbt, conn);

                    LogManager lm = new LogManager(conn);

                    lm.log(user, lxid, "0", "lxcz", stsLabel);

                    /*上报*/
                    if (status.equals("3")) {
                    }

                    conn.commit();

                    request.setAttribute("result", "{success: true}");
                }
            }
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-3'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }

    /**
     * 来信办理领导批示
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward check(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String status = StringTool.checkString(request.getParameter("SHSTATUS"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("SHSTATUS"));

                    if (orgsts.equals("2")) {
                        /*已审批事项,不能再次审批*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

                        String ldps = (String) cbh.getResource().get("LDPS");

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_LX");

                        if (cbh.getResource().containsKey(AnnexItem.ANNEX)) {
                            sbt.getResource().put(AnnexItem.ANNEX,
                                cbh.getResource().get(AnnexItem.ANNEX));
                            sbt.getResource().put("LDPSJ", "LDPSJ");
                            sbt.getResource().put("JBD", "JBD");
                        }

                        sbt.getResource().put("SHSTATUS", status);

                        sbt.getResource().put("LDPS", ldps);

                        sbt.getResource().put("lxid", lxid);

                        String stsLabel = "checking";

                        if (status.equals("2")) {
                            stsLabel = "checkyes";
                            sbt.getResource().put("BLSTATUS", "4");
                        } else if (status.equals("-1")) {
                            stsLabel = "checkno";
                            sbt.getResource().put("BLSTATUS", "-1");
                        }

                        BaseworkUtil util = new BaseworkUtil();

                        util.manualUpdate1(sbt, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", stsLabel, "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信科室办理
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward handle(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        String backInfo = StringTool.checkString(request.getParameter(
                    "H_BACKINFO"));
        
        String status = StringTool.checkString(request.getParameter(
        "H_STATUS"));
        
        String clfsid = StringTool.checkString(request.getParameter("CLFSID"));

        String clfs = StringTool.checkString(request.getParameter("CLFS"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("LXSTATUS"));
                    

                    if (orgsts.equals("1")) {
                        /*已办理事项,不能重复办理*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String sql = "SELECT HID FROM APP_XFHANDLER WHERE XFID = ? AND XF_TYPE = ?";

                        DataTrimmerI trimmer = new DataTrimmerI(conn);

                        DataTrimmerI.DataItemList dil = trimmer.createDIL();

                        dil.set(lxid);
                        dil.set(0);

                        Map hdlMap = trimmer.searchSingleData(sql, dil);

                        String hdlkey = StringTool.checkString(hdlMap.get("HID"));

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_XFHANDLER");

                        sbt.getResource().put("H_STATUS", status);
                        sbt.getResource().put("H_BACKINFO", backInfo);
                        sbt.getResource().put("H_DATE", DateTrimmer.getYMDHMS());
                        sbt.getResource().put("HID", hdlkey);
                        sbt.getResource().put("H_USERID", user.id());
                        sbt.getResource().put("H_USERNAME", user.iname());

                        BaseworkUtil util = new BaseworkUtil();

                        util.manualUpdate1(sbt, conn);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "1");
                        
                        sb.getResource().put("CLFSID", clfsid);

                        sb.getResource().put("CLFS", clfs);

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxhandle", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复查
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LXID"));

                String status = StringTool.checkString(info.get("FCSTATUS"));

                if (!status.equals("-1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "不能重复进行复查办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxfc");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复查派遣
     * <p>
     *    安排来信复查办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward savefc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("FCSTATUS"));

                    if (orgsts.equals("0")) {
                        /*已复查事项,不能再次复查*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String fcUserId = StringTool.checkString(request.getParameter(
                                    "FC_USERID"));

                        String fcBLDate = StringTool.checkString(request.getParameter(
                                    "FC_DATE"));

                        UserManager um = new UserManager(conn);

                        /*复查办理人*/
                        IUser fcUser = um.getUserById(fcUserId);

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_XFFC");

                        sbt.getResource().put("XFID", lxid);
                        sbt.getResource().put("XF_TYPE", "0");
                        sbt.getResource().put("FC_STATUS", "0");
                        sbt.getResource().put("FC_USERID", fcUser.id());
                        sbt.getResource().put("FC_USERNAME", fcUser.iname());
                        sbt.getResource().put("FC_GROUPID", fcUser.igroup().id());
                        sbt.getResource().put("FC_GROUPNAME",
                            fcUser.igroup().iname());
                        sbt.getResource().put("FC_DATE", fcBLDate);

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualSave1(sbt, conn);

                        String foid = (String) fcMap.get("FCID");

                        ObjectManager om = new ObjectManager(conn);
                        om.createSystemRelationObject(id, "APP_XFFC", foid);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "0");

                        sb.getResource().put("FCSTATUS", "0");

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        PurviewManager pm = new PurviewManager(conn);

                        /*授权信访复查负责人可对来信进行复查办理*/
                        pm.insert(object.id(), object.iname(), object.ikey(),
                            "ISYS_OBJECT", 3, fcUser.id(), fcUser.iname(),
                            "uesr", user, false);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxfccz", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复查
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward blfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            ObjectManager om = new ObjectManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else if (om.purview(id, user) < 2) {
                request.setAttribute("error", "您没有复查办理权限,无法进行复查办理");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LXID"));

                String status = StringTool.checkString(info.get("FCSTATUS"));

                if (status.equals("-1")) {
                    request.setAttribute("error", "该来信不能进行复查办理");

                    forward = mapping.findForward("error");
                } else if (status.equals("1")) {
                    request.setAttribute("error", "来信已经复查办理结束,不能重复复查");

                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxfcbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复查办理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward saveblfc(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        String id = StringTool.checkString(cbh.getResource().get("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("FCSTATUS"));

                    if (orgsts.equals("1")) {
                        /*已办理复查事项,不能重复复查*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        Table table = object.iobject().itable("APP_XFFC");

                        Map fcInfo = (Map) ((List) object.icontent(table)).get(0);

                        String fcid = StringTool.checkString(fcInfo.get("FCID"));

                        SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFC",
                                cbh);

                        sbt.getResource().put("FCID", fcid);
                        sbt.getResource().put("FC_STATUS", "1");
                        sbt.getResource().put("FC_BLDATE",
                            DateTrimmer.getYMDHMS());

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualUpdate1(sbt, conn);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "1");

                        sb.getResource().put("FCSTATUS", "1");

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxfcbl", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复核
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward editfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LXID"));

                String status = StringTool.checkString(info.get("FHSTATUS"));

                if (!status.equals("-1")) {
                    /*已办理事项,不能再次办理*/
                    request.setAttribute("error", "不能重复进行复核办理");
                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxfh");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复核派遣
     * <p>
     *    安排来信复核办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward savefh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("FHSTATUS"));

                    if (orgsts.equals("0")) {
                        /*已复核事项,不能再次复核*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        String fhUserId = StringTool.checkString(request.getParameter(
                                    "FH_USERID"));

                        String fhBLDate = StringTool.checkString(request.getParameter(
                                    "FH_DATE"));

                        UserManager um = new UserManager(conn);

                        /*复核办理人*/
                        IUser fcUser = um.getUserById(fhUserId);

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_XFFH");

                        sbt.getResource().put("XFID", lxid);
                        sbt.getResource().put("XF_TYPE", "0");
                        sbt.getResource().put("FH_STATUS", "0");
                        sbt.getResource().put("FH_USERID", fcUser.id());
                        sbt.getResource().put("FH_USERNAME", fcUser.iname());
                        sbt.getResource().put("FH_GROUPID", fcUser.igroup().id());
                        sbt.getResource().put("FH_GROUPNAME",
                            fcUser.igroup().iname());
                        sbt.getResource().put("FH_DATE", fhBLDate);

                        BaseworkUtil util = new BaseworkUtil();

                        Map fcMap = util.manualSave1(sbt, conn);

                        String foid = (String) fcMap.get("FHID");

                        ObjectManager om = new ObjectManager(conn);
                        om.createSystemRelationObject(id, "APP_XFFH", foid);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "0");

                        sb.getResource().put("FHSTATUS", "0");

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        PurviewManager pm = new PurviewManager(conn);

                        /*授权信访复查负责人可对来信进行复查办理*/
                        pm.insert(object.id(), object.iname(), object.ikey(),
                            "ISYS_OBJECT", 3, fcUser.id(), fcUser.iname(),
                            "uesr", user, false);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxfhcz", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复核
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward blfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        String id = StringTool.checkString(request.getParameter("id"));

        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectLCManager manager = new ObjectLCManager(conn);

            ObjectManager om = new ObjectManager(conn);

            IObject object = manager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("error", "来信登记信息并不存在");

                forward = mapping.findForward("error");
            } else if (om.purview(id, user) < 2) {
                request.setAttribute("error", "您没有复核办理权限,无法进行复核办理");

                forward = mapping.findForward("error");
            } else {
                Table tbl = object.iobject().itable("APP_LX");

                Map info = (Map) ((List) object.icontent(tbl)).get(0);

                String lxid = StringTool.checkString(info.get("LXID"));

                String status = StringTool.checkString(info.get("FHSTATUS"));

                if (status.equals("-1")) {
                    request.setAttribute("error", "该来信不能进行复核办理");

                    forward = mapping.findForward("error");
                } else if (status.equals("1")) {
                    request.setAttribute("error", "来信已经复核办理结束,不能重复复核");

                    forward = mapping.findForward("error");
                } else {
                    request.setAttribute("object", object);
                    request.setAttribute("wobject", object.iobject());
                    forward = mapping.findForward("lxfhbl");
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            forward = mapping.findForward("error");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 来信复核办理
     * <p>
     *    安排来信复核办理人员
     * </p>
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward saveblfh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActionForward forward = null;

        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        String id = StringTool.checkString(cbh.getResource().get("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            ObjectManager manager = new ObjectManager(conn);

            ObjectLCManager lcmanager = new ObjectLCManager(conn);

            IObject object = lcmanager.getObjectById(id);

            if (object.iobject() == null) {
                request.setAttribute("result",
                    "{success: false, errCode : '-1'}");
            } else {
                IUser user = ISystem.getSessionUser(request);

                if (manager.purview(id, user) < 2) {
                    request.setAttribute("result",
                        "{success: false, errCode : '-2'}");
                } else {
                    Table tbl = object.iobject().itable("APP_LX");

                    Map info = (Map) ((List) object.icontent(tbl)).get(0);

                    String lxid = StringTool.checkString(info.get("LXID"));

                    String orgsts = StringTool.checkString(info.get("FHSTATUS"));

                    if (orgsts.equals("1")) {
                        /*已办理复核事项,不能重复复核*/
                        request.setAttribute("result",
                            "{success: false, errCode : '-3'}");
                    } else {
                        Table table = object.iobject().itable("APP_XFFH");

                        Map fhInfo = (Map) ((List) object.icontent(table)).get(0);

                        String fhid = StringTool.checkString(fhInfo.get("FHID"));

                        SimpleBean4Table sbt = new SimpleBean4TableImpl("APP_XFFH",
                                cbh);

                        sbt.getResource().put("FHID", fhid);
                        sbt.getResource().put("FH_STATUS", "1");
                        sbt.getResource().put("FH_BLDATE",
                            DateTrimmer.getYMDHMS());

                        BaseworkUtil util = new BaseworkUtil();

                        Map fhMap = util.manualUpdate1(sbt, conn);

                        SimpleBean4Table sb = new SimpleBean4TableImpl("APP_LX");

                        sb.getResource().put("LXSTATUS", "1");

                        sb.getResource().put("FHSTATUS", "1");

                        sb.getResource().put("lxid", lxid);

                        util.manualUpdate1(sb, conn);

                        LogManager lm = new LogManager(conn);

                        lm.log(user, lxid, "0", "lxfhbl", "");

                        conn.commit();

                        request.setAttribute("result", "{success: true}");
                    }
                }
            }

            forward = mapping.findForward("result");
        } catch (Exception ex) {
            conn.rollback();

            request.setAttribute("result", "{success: false, errCode : '-4'}");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
}
